/**
 ******************************************************************************
 * @file    lv_100ask_demo_course_3_2_1.c
 * @author  百问科技
 * @version V1.0
 * @date    2022-01-20
 * @brief	3_2_1 课的课堂代码
 ******************************************************************************
 * Change Logs:
 * Date           Author          Notes
 * 2022-01-20     zhouyuebiao     First version
 ******************************************************************************
 * @attention
 *
 * Copyright (C) 2008-2021 深圳百问网科技有限公司<https://www.100ask.net/>
 * All rights reserved
 *
 ******************************************************************************
 */


/*********************
 *      INCLUDES
 *********************/
#include "../../lv_100ask_teach_demos.h"
#undef      LV_USE_100ASK_DEMO_COURSE_3_2_1
#define  LV_USE_100ASK_DEMO_COURSE_3_2_1        1
#if LV_USE_100ASK_DEMO_COURSE_3_2_1

#include "lv_100ask_demo_course_3_2_1.h"
#include <windows.h>

//#include "../../../lv_lib_bmp/lv_bmp.h"

//#include "../../../lv_lib_jpg/lv_sjpg.h"
#include "../../../lv_lib_avi/avilib.h"

#include "../../../lvgl/examples/libs/gif/lv_example_gif.h"

/*********************
 *      DEFINES
 *********************/


/**********************
 *  STATIC VARIABLES
 **********************/
 /**
 * Open a BMP file from a file
 */
 #if   1
 static lv_img_dsc_t IMG1 = {
    .header.cf = LV_IMG_CF_RAW,
    .header.always_zero = 0,
    .header.reserved = 0,
    .header.w = 0,
    .header.h = 0,
    .data_size = 0,
    .data = NULL,
};
void lv_example_jpg_1(void)
{

    FILE* pFile;
    long lSize;

    pFile = fopen("./avi/1.jpg", "rb");
    if (pFile == NULL) {
        printf("read fail.");
        return;
    }
    /* 获取文件大小 */
    fseek(pFile, 0, SEEK_END);
    lSize = ftell(pFile);
    rewind(pFile);

    uint8_t* buffer = malloc(lSize);

    /* 将文件拷贝到buffer中 */
    fread(buffer, 1, lSize, pFile);
    fclose(pFile);

    IMG1.data_size = lSize;
    IMG1.data = buffer;

    //ESP_LOG_BUFFER_HEX(TAG, buffer, 10);
    lv_obj_t* scr = lv_scr_act();
    lv_obj_set_style_bg_img_src(scr, &IMG1, LV_PART_MAIN);
    lv_obj_set_style_bg_img_opa(scr, LV_OPA_COVER, LV_PART_MAIN);
    free(buffer);

}

void lv_example_jpg_2(void)
{
    //lv_split_jpeg_init();

	lv_obj_t * objpg =  lv_img_create(lv_scr_act());				// 创建一个IMG对象
	// 加载SD卡中的JPG图片
	lv_img_set_src(objpg,"D:/avi/1.jpg");
	//lv_obj_align(objpg, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);			// 重新设置对齐

    lv_obj_center(objpg);

}
static void btn_toggle_event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_target(e);        // 获取触发事件的部件(对象)
    lv_event_code_t code = lv_event_get_code(e);    // 获取当前部件(对象)触发的事件代码

    switch(code){
        case LV_EVENT_VALUE_CHANGED:
            printf("LV_EVENT_VALUE_CHANGED\n");
            break;
        default:
            //printf("NONE\n");
            break;
    }
}
avi_t * avi;

#include <stdio.h>
lv_img_dsc_t img_dsc = {
	.header.always_zero = 0,
	.header.w = 320,
	.header.h = 240,
	.data_size = 320 * 240 * 4,
	//.header.cf = LV_IMG_CF_TRUE_COLOR,
	.header.cf = LV_IMG_CF_RAW,
	.data = NULL,
};
void  TfxSleep(unsigned long ulMilliSeconds);
void    MyDelay(unsigned long ulMilliSeconds)
{
    unsigned long oldtick = GetTickCount64();
    MSG msg;
    while(1){
        if(GetTickCount64() - oldtick >= ulMilliSeconds){
            break;
        }
        TfxSleep(1);
        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) //GetMessage阻塞等待，直到有消息到来才有返回值
        {
            TranslateMessage(&msg);
            DispatchMessage (&msg);
        }

    }
}
void lv_example_avi(void)
{
    lv_split_jpeg_init();

    char frameBuffer[60*1024];
    long frameSize = 0;
    int keyFrame = 0;
    long pos,frames;
    char *ff="./avi/hc16s_1.avi";
    avi = AVI_open_input_file(ff, 1);

    if(avi != NULL){
        LV_LOG_USER("W: %d,H: %d", avi->width,avi->height);
        //将avi帧byte读取到bitmap中
        pos = 0;
        frames=AVI_video_frames(avi);

        if(pos<frames)
        {
            AVI_set_video_position((avi_t *) avi,pos);
                frameSize = AVI_read_frame((avi_t *) avi, frameBuffer, &keyFrame);
            LV_LOG_USER("pos:%d,frameSize: %d",pos, frameSize);
            //if(frameSize > 1){

                lv_obj_t * objpg =  lv_img_create(lv_scr_act());				// 创建一个IMG对象

                img_dsc.data = frameBuffer;
                img_dsc.data_size =frameSize;
                lv_img_set_src(objpg,&img_dsc );					// 加载SD卡中的JPG图片
                //lv_obj_align(objpg, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);			// 重新设置对齐

                //lv_obj_center(objpg);


        }
        LV_LOG_USER( "exit while.");

    }
    AVI_close((avi_t *) avi);

}
#endif

//void lv_example_gif_1(void);
void lv_100ask_demo_course_3_2_1(void)
{
#if  0
    /* 创建一个btn部件(对象) */
    lv_obj_t * btn = lv_btn_create(lv_scr_act());       // 创建一个btn部件(对象),他的父对象是活动屏幕对象

    // 修改按钮部件（对象）矩形背景部分的样式
    lv_obj_set_style_bg_color(btn, lv_color_hex(0x1e1e1e), LV_PART_MAIN | LV_STATE_PRESSED);

    // 打开了 LV_OBJ_FLAG_CHECKABLE ，当对象被点击时有选中切换(Toggle)状态的效果
    // 其触发的是 LV_EVENT_VALUE_CHANGED 事件类型
    lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);

    // 处理 LV_EVENT_VALUE_CHANGED 事件类型示例
    lv_obj_add_event_cb(btn, btn_toggle_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
#endif
    //
     //lv_example_jpg_1();
     //lv_example_jpg_2();
    lv_example_avi();
}
   #if   0
        /////////////////////////////////////////写入
            char    *fout="./avi/hc002.jpg" ;
            FILE* f = fopen(fout, "wb");
            if (f == NULL) {
                LV_LOG_USER( "Failed to open file for writing");
                return;
            }
            //fprintf(f, "Hello file IO  %s!\n", card->cid.name);
                //进行写入数据
            fwrite(frameBuffer,frameSize,1,f);
            fclose(f);
#endif // 0
#endif /* LV_USE_100ASK_DEMO_COURSE_3_2_1 */



       #if   0
            if((pos % 30 )==0){

            /////////////////////////////////////////写入
                char    fout[64];
                sprintf(fout,"./avi/hc%d.jpg",pos) ;
                FILE* f = fopen(fout, "wb");
                if (f == NULL) {
                    LV_LOG_USER( "Failed to open file for writing");
                    return;
                }
                //fprintf(f, "Hello file IO  %s!\n", card->cid.name);
                    //进行写入数据
                fwrite(frameBuffer,frameSize,1,f);
                fclose(f);
            }
    #endif // 0
